package com.TP.www.service.impl;

import com.TP.www.service.AssetPort;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 用户金额功能实现类
 */
public class CustomerAsset implements AssetPort {
    /**
     * 增加余额
     * @param num 账户余额增加的金额
     */
    @Override
    public void add(double num){
        double totalNum = loginUser.getAsset() + num;
        loginUser.setAsset(totalNum);
        AssetPort.refresh(loginUser,totalNum);
    }


    /**
     * 用户充值
     */
    public void recharge(){
        while(true){
            try {
                String num;
                double max = 10000;
                System.out.println("**************");
                System.out.println("请输入充值金额(单次最高金额为一万):");
                num = SYS_SC.nextLine();
                while ( Double.parseDouble(num) > max || !num.matches(ASSET_REGULAR)){
                    System.out.println("金额有误!");
                    System.out.println("请重新输入充值金额(单次最高金额为一万):");
                    num = SYS_SC.nextLine();
                }
                add(Double.parseDouble(num));
                System.out.println("充值成功，当前余额为" + loginUser.getAsset() + "元");
                System.out.println("**************");
                break;
            }catch (NumberFormatException e){
                System.out.println("输入金额有误请重新输入....");
            }
        }
    }
}
